<?php

/**
 * sfSocialContactRequest form.
 *
 * @package    edmuch
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrinePluginFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sfSocialContactRequestForm extends PluginsfSocialContactRequestForm
{
  public function configure()
  {
	  // hide unuseful fields
    unset($this['created_at'], $this['updated_at'], $this['user_from'], $this['accepted']);

    // make "message" a textarea
		$this->widgetSchema['message'] = new sfWidgetFormTextarea();

    // current user's id
    $this->widgetSchema['user_from'] = new sfWidgetFormInputHidden();
    $this->setDefault('user_from', $this->options['user']->getId());
    $this->setValidator('user_from', new sfValidatorChoice(array('choices' => array($this->options['user']->getId()))));

    // exclude contacts from user_to TODO query is correct, but choices are not !?! :-|
    $q = Doctrine_Query::create()
      ->from('sfGuardUser u')
      ->whereNotIn('u.id', array_merge($this->options['user']->getContactIds(), array($this->options['user']->getId())))
      ->orderBy('u.username')
      ->limit(50);
    $this->widgetSchema['user_to']->setOption('query', $q);
    $this->widgetSchema['user_to']->setOption('add_empty', true);
    $this->widgetSchema['user_to']->setLabel('To');

    // possible selected user
    if (!empty($this->options['to']))
    {
      $this->setDefault('user_to', $this->options['to']->getId());
    }

    // request can't be sent to a contact
    $this->widgetSchema['user_from']->setLabel('Error');  // trick!
    $this->validatorSchema->setPostValidator(
      new sfValidatorAnd(
        array(
          new sfValidatorDoctrineUnique(array('model' => 'sfSocialContactRequest',
                                              'column' => array('user_from', 'user_to')),
                                        array('invalid'=> 'Request already sent.')),
          new sfValidatorDoctrineUnique(array('model' => 'sfSocialContact',
                                              'column' => array('user_from', 'user_to')),
                                        array('invalid'=> 'User is already a contact.'))
    )));
  }
}
